package com.gzy.wbapplication.utils;

import android.content.Context;
import android.content.SharedPreferences;

import static android.content.Context.MODE_PRIVATE;

public class SPUtils {

    private static final String appdata = "appdata";

    // 存储
    public static void save(Context context, String key, String value){
        //步骤2-1：创建一个SharedPreferences.Editor接口对象，lock表示要写入的XML文件名，MODE_WORLD_WRITEABLE写操作
        SharedPreferences.Editor editor = context.getSharedPreferences(appdata, MODE_PRIVATE).edit();
        //步骤2-2：将获取过来的值放入文件
        editor.putString(key, value);
        //步骤3：提交
        editor.commit();
    }
    // 读取
    public static String read(Context context,String key){
        //步骤2-1：创建一个SharedPreferences.Editor接口对象，lock表示要写入的XML文件名，MODE_WORLD_WRITEABLE写操作
        SharedPreferences sharedPreferences = context.getSharedPreferences(appdata, MODE_PRIVATE);
        return sharedPreferences.getString(key, "");
    }
    // 清除所有数据
    public static void clear(Context context){
        SharedPreferences.Editor editor = context.getSharedPreferences(appdata, MODE_PRIVATE).edit();
        editor.clear();
        editor.commit();
    }
    // 删除指定键的数据
    public static void remove(Context context,String key){
        SharedPreferences.Editor editor = context.getSharedPreferences(appdata, MODE_PRIVATE).edit();
        editor.remove(key);
        editor.commit();
    }
}

